﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public delegate void AddressClickEventHandler(object sender, EventArgs e);

public partial class CustomerAddress : System.Web.UI.UserControl
{
    private int intCustomerID = -1;
    private int intCommand = -1;
    private bool isAudit = false;
    private bool isUpdate = false;

    public bool IsAudit
    {
        get { return isAudit; }
        set { isAudit = value; }
    }
    public int CustomerID
    {
        get { return intCustomerID; }
        set { intCustomerID = value; }
    }
    public int Command
    {
        get { return intCommand; }
        set { intCommand = value; }
    }
    public string No
    {
        get { return this.txtNo.Text; }
        set { this.txtNo.Text = value; }
    }
    public string Name
    {
        get { return this.txtName.Text; }
        set { this.txtName.Text = value; }
    }
    public string Desc
    {
        get { return this.txtDesc.Text; }
        set { this.txtDesc.Text = value; }
    }
    public string Zip
    {
        get { return this.txtZip.Text; }
        set { this.txtZip.Text = value; }
    }
    public int IsStop
    {
        get { return Int32.Parse(this.dropState.SelectedItem.Value); }
        set { this.dropState.SelectedIndex = value; }
    }
    public bool IsDefault
    {
        get { return this.chkDefault.Checked; }
        set { this.chkDefault.Checked = value; }
    }
    public bool AddPanelVisible
    {
        get { return this.panAddResult.Visible; }
        set { this.panAddResult.Visible = value; }
    }
    public bool IsUpdate
    {
        get { return this.isUpdate; }
        set { this.isUpdate = value; }
    }

    public event AddressClickEventHandler AddClick;
    public event AddressClickEventHandler CalcenClick;
    public event AddressClickEventHandler UpdateClick;

    protected void OnClick(EventArgs e)
    {
        if (AddClick != null) AddClick(this, e);
        if (CalcenClick != null) CalcenClick(this, e);
        if (UpdateClick != null) UpdateClick(this, e);
    }

    public void ChangPage(object sender, EventArgs e)
    {
        this.OnClick(e);

        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetChildResultDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindCustomersAddress(dst, intPageIndex);
        }
    }

    public void UpdateInfo(object sender, ImageClickEventArgs e)
    {
        this.OnClick(e);
        //this.GetCustomersPriceInfo(this.CustomerID);

        ImageButton ibtn = (ImageButton)sender;
        int intCommand = Int32.Parse(ibtn.CommandName);
        int intIndex = Int32.Parse(ibtn.CommandArgument);
        int intID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());

        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        CuTextBox txtRsNo = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsNo");
        CuTextBox txtRsName = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsName");
        CuTextBox txtRsDesc = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsDesc");
        CuTextBox txtRsZip = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsZip");
        CuTextBox txtRsState = (CuTextBox)this.dgrdResult.Items[intIndex].FindControl("txtRsState");
        CheckBox chkRsDefault = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsDefault");
        DropDownList dropRsState = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("dropRsState");

        if (intCommand == 0)//打开编辑
        {
            chkRsDefault.Enabled = true;
            chkRsSelect.Visible = ibtnRsEdit.Visible = txtRsState.Visible = false;
            ibtnRsCancel.Visible = ibtnRsServer.Visible = dropRsState.Visible = true;
            txtRsNo.ReadOnly = txtRsName.ReadOnly = txtRsDesc.ReadOnly = txtRsZip.ReadOnly = false;
            this.lblJs.Text = "<script>document.getElementById('" + txtRsNo.ClientID + "_txtBox').focus();document.getElementById('" + txtRsNo.ClientID + "_txtBox').select();</script>";
        }
        else if (intCommand == 2)//保存
        {
            if (txtRsNo.Text.Trim() != "" && txtRsName.Text.Trim() != "" && txtRsDesc.Text.Trim() != "" && txtRsZip.Text.Trim() != "")
            {
                ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerAddress(intID, this.CustomerID, txtRsNo.Text.Trim(), txtRsName.Text.Trim(), txtRsDesc.Text.Trim(), txtRsZip.Text.Trim(), chkRsDefault.Checked, Int32.Parse(dropRsState.SelectedItem.Value));

                if (is_Succeed)
                {
                    this.isUpdate = false;
                }
                else
                {
                    this.lblJs.Text = "<script>alert('修改客户收货地址失败，请重试！');</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('必填项不能为空！');</script>";
                this.isUpdate = false;
            }
        }
        else
        {
            this.isUpdate = false;
        }
    }

    private void DeleteInfo(int Cus_ID, int Type)
    {
        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
        int intCount = this.dgrdResult.Items.Count - 1;

        for (int i = intCount; i >= 0; i--)
        {
            bool isChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

            if (isChecked)
            {
                int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());
                Boolean is_Succeed = cls_Customer.DeleteCustomerAddress(intID, Type);
            }
        }
    }

    private void GetCustomersAddressInfo(int Cus_ID)
    {
        ClsCustomersInfoDAL cls_Customers = new ClsCustomersInfoDAL();
        DataSet dst = cls_Customers.GetCustomersAddress(Cus_ID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetChildResultDataSet = dst;

            this.BindCustomersAddress(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
        }
    }

    private void BindCustomersAddress(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.lblJs.Text = "";//清空Js脚本
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (this.intCommand == 0)//新增
            {
                this.panAddResult.Visible = true;
                this.lblJs.Text = "<script>document.getElementById('" + this.txtNo.ClientID + "_txtBox').focus();</script>";
            }
            else if (this.intCommand == 1)//作废
            {
                this.DeleteInfo(this.intCustomerID, 0);
            }
            else if (this.intCommand == 2)//彻底作废
            {
                this.DeleteInfo(this.intCustomerID, 1);
            }

            if (!this.isUpdate)
            {
                this.GetCustomersAddressInfo(this.intCustomerID);
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnAddCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.OnClick(e);
    }

    protected void ibtnAddServer_Click(object sender, ImageClickEventArgs e)
    {
        this.OnClick(e);
        this.GetCustomersAddressInfo(this.intCustomerID);
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            CuTextBox txtRsState = (CuTextBox)e.Item.FindControl("txtRsState");
            CheckBox chkRsDefault = (CheckBox)e.Item.FindControl("chkRsDefault");
            DropDownList dropRsState = (DropDownList)e.Item.FindControl("dropRsState");

            //从表可操作性控制
            if (IsAudit)
            {
                chkRsSelect.Enabled = ibtnRsEdit.Enabled = false;
                ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            }

            //是否默认
            bool is_Default = bool.Parse(e.Item.Cells[2].Text.Trim());
            chkRsDefault.Checked = is_Default;

            //状态
            bool is_Stop = bool.Parse(e.Item.Cells[3].Text.Trim());
            dropRsState.SelectedIndex = (is_Stop) ? 1 : 0;
            txtRsState.Text = dropRsState.SelectedItem.Text;
        }
    }
}
